package larpplanner.gui;

import java.util.ArrayList;
import java.util.List;

import larpplanner.database.FicCharacter;

import com.cloudgarden.resource.SWTResourceManager;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;

public class NewCharWin extends org.eclipse.swt.widgets.Dialog {

	private GUISafeLARPManager mngr = GUISafeLARPManager.get();

	private FicCharacter ficChar;
	
	private Shell dialogShell;
	private Button okBtn;
	private Text powersTxt;
	private Label powersLbl;
	private Text speciesTxt;
	private Label speciesLbl;
	private Button genderCheckBoxFemale;
	private Button genderCheckBoxMale;
	private Label genderLbl;
	private Text charNameTxt;
	private Text univNameTxt;
	private Label univNameLbl;
	private Label charNameLbl;

	/**
	 *	use this CTOR when adding a new character 
	 */
	public NewCharWin(Shell parent, int style) {
		super(parent, style);
		ficChar = new FicCharacter();
		initGui();
		initLogic();
	}
	
	private void initLogic() {
		okBtn.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				// validity checks
				if(charNameTxt.getText().length() < 3) {
					PopMessage.notifyMessage("enter character name with atleast 3 characters");
					return;
				}else if(univNameTxt.getText().length() < 3) {
					PopMessage.notifyMessage("enter universe name with atleast 3 characters");
					return;
				}
				
				// prepare the fic char
				ficChar.setName(charNameTxt.getText());
				
				List<String> universes = new ArrayList<String>();
				universes.add(univNameTxt.getText());
				ficChar.setUniverses(universes);
				
				List<String> powers = new ArrayList<String>();
				for(String s : powersTxt.getText().split(","))
					powers.add(s.trim());
				ficChar.setPowers(powers);
				
				List<String> species = new ArrayList<String>();
				for(String s : speciesTxt.getText().split(","))
					species.add(s.trim());
				ficChar.setSpecies(species);
				
				
				// send to DB
				mngr.add(FicCharacter.class, ficChar, new SafeCallback<Long>() {
					public void onReturn(Long result) {
						if(result == -1)
							PopMessage.errorMessage("error adding the character");
						dialogShell.dispose();
					}
				});
			}
		});
		
		genderCheckBoxFemale.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ficChar.setGender("Female");
			}
		});
		
		genderCheckBoxMale.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ficChar.setGender("Male");
			}
		});
	}
	
	public static void main(String[] args) {
		try {
			Display display = Display.getDefault();
			Shell shell = new Shell(display);
			NewCharWin inst = new NewCharWin(shell, SWT.NULL);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initGui() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
			SWTResourceManager.registerResourceUser(dialogShell);
			
			GridLayout dialogShellLayout = new GridLayout();
            dialogShellLayout.numColumns = 3;
            dialogShellLayout.marginTop = 5;
            dialogShellLayout.marginRight = 5;
            dialogShellLayout.marginLeft = 5;
            dialogShellLayout.marginBottom = 5;
            dialogShell.setLayout(dialogShellLayout);
            dialogShell.layout();
            dialogShell.pack();            
            dialogShell.setSize(490, 187);
            dialogShell.setText("New Character");
            {
            	charNameLbl = new Label(dialogShell, SWT.NONE);
            	GridData charNameLblLData = new GridData();
            	charNameLbl.setLayoutData(charNameLblLData);
            	charNameLbl.setText("Character Name:");
            }
            {
            	charNameTxt = new Text(dialogShell, SWT.NONE);
            	GridData charNameTxtLData = new GridData();
            	charNameTxtLData.horizontalAlignment = GridData.FILL;
            	charNameTxtLData.grabExcessHorizontalSpace = true;
            	charNameTxtLData.horizontalSpan = 2;
            	charNameTxt.setLayoutData(charNameTxtLData);
            }
            {
            	univNameLbl = new Label(dialogShell, SWT.NONE);
            	GridData univNameLblLData = new GridData();
            	univNameLbl.setLayoutData(univNameLblLData);
            	univNameLbl.setText("Universe Name:");
            }
            {
            	univNameTxt = new Text(dialogShell, SWT.NONE);
            	GridData univNameTxtLData = new GridData();
            	univNameTxtLData.horizontalAlignment = GridData.FILL;
            	univNameTxtLData.grabExcessHorizontalSpace = true;
            	univNameTxtLData.horizontalSpan = 2;
            	univNameTxt.setLayoutData(univNameTxtLData);
            }
            {
            	powersLbl = new Label(dialogShell, SWT.NONE);
            	GridData powersLblLData = new GridData();
            	powersLbl.setLayoutData(powersLblLData);
            	powersLbl.setText("Powers: (seperate with commas)");
            }
            {
            	powersTxt = new Text(dialogShell, SWT.NONE);
            	GridData powersTxtLData = new GridData();
            	powersTxtLData.horizontalSpan = 2;
            	powersTxtLData.grabExcessHorizontalSpace = true;
            	powersTxtLData.horizontalAlignment = GridData.FILL;
            	powersTxt.setLayoutData(powersTxtLData);
            }
            {
            	speciesLbl = new Label(dialogShell, SWT.NONE);
            	GridData speciesLblLData = new GridData();
            	speciesLbl.setLayoutData(speciesLblLData);
            	speciesLbl.setText("Species: (seperate with commas)");
            }
            {
            	speciesTxt = new Text(dialogShell, SWT.NONE);
            	GridData speciesTxtLData = new GridData();
            	speciesTxtLData.horizontalSpan = 2;
            	speciesTxtLData.grabExcessHorizontalSpace = true;
            	speciesTxtLData.horizontalAlignment = GridData.FILL;
            	speciesTxt.setLayoutData(speciesTxtLData);
            }
            {
            	genderLbl = new Label(dialogShell, SWT.NONE);
            	GridData genderLblLData = new GridData();
            	genderLbl.setLayoutData(genderLblLData);
            	genderLbl.setText("Gender:");
            }
            {
            	genderCheckBoxFemale = new Button(dialogShell, SWT.RADIO | SWT.LEFT);
            	GridData genderCheckBoxFemaleLData = new GridData();
            	genderCheckBoxFemale.setLayoutData(genderCheckBoxFemaleLData);
            	genderCheckBoxFemale.setText("Female");
            }
            {
            	genderCheckBoxMale = new Button(dialogShell, SWT.RADIO | SWT.LEFT);
            	GridData genderCheckBoxMaleLData = new GridData();
            	genderCheckBoxMale.setLayoutData(genderCheckBoxMaleLData);
            	genderCheckBoxMale.setText("Male");
            }
			{
				okBtn = new Button(dialogShell, SWT.NONE);
				GridData okBtnLData = new GridData();
				okBtnLData.heightHint = 25;
				okBtnLData.horizontalAlignment = GridData.FILL;
				okBtnLData.horizontalSpan = 3;
				okBtn.setLayoutData(okBtnLData);
				okBtn.setText("OK");
				okBtn.setSize(60, 25);
			}
		} catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	public void open() {
		try{
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();
			while (!dialogShell.isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
